//
//  TopViewController.m
//  项目1 CLCMovie
//
//  Created by huiwenjiaoyu on 15/7/17.
//  Copyright (c) 2015年 CLC. All rights reserved.
//

#import "TopViewController.h"

@interface TopViewController ()

@end

@implementation TopViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self _loadData];
    [self.navigationItem setTitle:@"TOP10"];
    [self _creatCollectionView];
    // Do any additional setup after loading the view.
}


#pragma mark - 创建CollectionView

- (void)_creatCollectionView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    
    layout.minimumInteritemSpacing = 5;
    layout.minimumLineSpacing = 5;
    layout.itemSize = CGSizeMake((kWidth-20)/3, 200);
    
    CollcView = [[UICollectionView alloc] initWithFrame:CGRectMake(5, 0, kWidth-10, kHieght-108) collectionViewLayout:layout];
    
    UINib *nib = [UINib nibWithNibName:@"TopCellView" bundle:[NSBundle mainBundle]];
    [CollcView registerNib:nib forCellWithReuseIdentifier:@"topCell"];
    
    CollcView.delegate = self;
    CollcView.dataSource = self;
    
    [self.view addSubview:CollcView];

}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [dataArray count];
}

- (UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    TopCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"topCell" forIndexPath:indexPath];
   
    [cell setTopModal:dataArray[indexPath.row]];
    return cell;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(10, 0, 10, 0);
}


#pragma mark - 加载数据
- (void)_loadData
{
    dataArray = [[NSMutableArray alloc] init];
    
    NSDictionary* dic = [DataService getArrayOrDictionNaryfromJosnWithNamed:@"top250"];
    NSArray* SubArray = [NSArray arrayWithArray:[dic valueForKey:@"subjects"]];
    
    for (int i = 0; i < SubArray.count; i++) {
        NSString* title = [SubArray[i] valueForKey:@"title"];
        float mark = [[[SubArray[i] valueForKey:@"rating"] valueForKey:@"average"] floatValue];
        NSDictionary* img = [NSDictionary dictionaryWithDictionary:[SubArray[i] valueForKey:@"images"]];
        
        TopModal* modal = [[TopModal alloc] initWithTitle:title andImage:img andMark:mark];
        
        [dataArray addObject:modal];
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
